<?php
include_once "../../qc_base.php";
include_once "../../qc_utils.php";
include_once "../../bootstrap.php";

include_once "../../classes/MakePage.php";


include '../actionsupport.php';

//
function write_file($crudset_id){

	// get data

	$q = Doctrine_Query::create()
	->from('Crudsets cs')
	->innerJoin('cs.Crudcols cc')
	->leftJoin('cs.Projects pro')	
	->leftJoin('cc.VarInfo v')
	->Where('crudset_id = ?',$crudset_id);

	$ar_info = $q->execute()->toArray(true);

	
	// get the main table for the set
	$qtb = Doctrine_Query::create()
	->from('Tablesources tb')->where('tablesource_id = ?',$ar_info[0]['main_table']);
	//$tablesource = $qtb->fetchOne();
	$tablesource = $qtb->execute()->toArray(true); 
	
	
	$table_name = $tablesource[0]['table_name'];	
	

/*
--------------- Structure Data Format in Array $ar_info  (With Example Data)
(Each Row of Crudsets)	
        (
            [crudset_id] => 6
            [cs_name] => payments
            [paging] => 0
            [sorting] => 1
            [filter] => 1
            [main_table] => 82
            [project_id] => 1
            [query] => 
            [cs_type] => inline_edi
            [list_base_name] => def_list
            [proc_base_name] => CFprocess
            [process_class] => process
            [class_base_name] => CF_AJTable
            [html_table_name] => html_table
            [class_name] => CListSpecial
            [list_table_instance] => tableinstance
            [proc_class_instance] => process
            [edit_base_class] => CFform
            [edit_class_name] => FormClass
            [edit_instance] => TheForm
            [edit_page_name] => 
            [multiact] => 0
            [css_file] => commonctls.css
            [showbuttons] => sd
            [subdirectory] => payments/
            [needs_login] => 0
            [Crudcols] => Array
                ( Each CrudCol )
                    [0] => Array
                        (
                            [crudcol_id] => 637
                            [crudset_id] => 6
                            [variable_id] => 599
                            [is_key] => 1
                            [shown_in_list] => 1
                            [edit_in_list] => 0
                            [edit_type] => text
                            [filter] => 1
                            [showfilter] => 0
                            [restrict] => 0
                            [is_sort] => 0
                            [header_name] => 
                            [is_add] => 0
                            [list_query] => 
                            [list_keycol] => 
                            [list_showcol] => 
                            [size] => 20
                            [rows] => 1
                            [col_order] => 0
                            [makefunction] => 
                            [Crudsets] => 
                            [VarInfo] => Array
                                (
                                    [variable_id] => 599
                                    [variable_name] => customerNumber
                                    [type] => int(11)
                                    [quote] => 
                                    [project_id] => 1
                                    [variable_size] => 
                                    [source_id] => 82
                                    [def_show_text] => 
                                    [is_key] => 1
                                )

                        )

                )
			
            [Projects] => Array
                (
                    [project_id] => 1
                    [project_name] => sampletest1
                    [def_function_page] => 
                    [project_path] => sampletest1
                    [project_server] => localhost
                    [project_db] => sampledb
                    [project_login] => root
                    [project_pass] => 
                    [class_subpath] => CFClass
                    [user_table_name] => users
                    [up_table_name] => users_pages
                    [dbmethod] => pdo
                    [dbtype] => mysql
                    [template_dir] => templates/default/
                    [file_structure] => standard_pdo
                )

        )

)

*/

// replace from template file

$str_tpl = file_get_contents('tpl_view.php');
$str_out = str_replace('<cs_name>',$ar_info[0]['cs_name'],$str_tpl);

$str_out = str_replace('<main_table>',$table_name,$str_out);

// write to file
$outputfile = get_output_path('control', $ar_info[0]['cs_name'], $ar_info[0]['Projects']['file_structure'], $ar_info[0]['Projects']['project_name']) . $ar_info[0]['cs_name'] . '_view.php';
file_put_contents($outputfile,$str_out);

print "wrote $outputfile \n";

}










?>